// pages/me/me.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: {
      nickName: '登录/注册',
      avatarUrl: '../../images/头像.png',
    },
    youhuijuan:10,
    userInfo:{},
    isLogin: false,
    isLoading:true
  },
  onLoad(){
    this.getUserInfo()
    let user = wx.getStorageSync('user');
      if(user){
        this.setData({
          user:user,
          isLogin: true
        })
      }

  },
  login() {
    let user;
    const res = wx.getUserProfile({
      desc: '用于登录餐厅小程序',
    }).then((res)=>{
      wx.showLoading({
        title: '登录ing',
      });
      user = res.userInfo
      // 设置微信的账号信息
      wx.setStorageSync('user', res.userInfo)
    }).then(()=>{
      return wx.login({})
    }).then((getCode)=>{
        wx.request({
          url: `https://api.weixin.qq.com/sns/jscode2session?appid=wxed71286a53f8785b&secret=7bde5996565f5ce1879a5562b185fce4&js_code=${getCode.code}&grant_type=authorization_code`,
        success:(res)=>{
          console.log('openId',res);
          // 设置微信账号的唯一openID
          wx.setStorageSync('openID',res.data.openid)
          this.setData({
            user,
            isLogin: true,
            // isLoading:false
          })
          wx.request({
            url:'http://localhost:5555/users/checkUser',
            method:'post',
            data:{
              "openid":res.data.openid,
              "nikename":user.nickName,
              "avatarUrl":user.avatarUrl,
              "gender":user.gender,
              "language":user.language
          }
          })
        }})
    }).catch((err)=>{
      wx.showToast({
        title:err,
        icon:'error'
      })
    }).finally(()=>{
      wx.hideLoading();

    })
  },
  outLogin() {
    console.log('退出');
    this.setData({
      user: {},
      isLogin: false
    })
    wx.removeStorage({
      key: 'user',
    })
    wx.removeStorage({
      key: 'openID',
    })

  },
  getUserInfo(){
    setTimeout(()=>{
      this.setData({
        isLoading:false
      })
    },1000)
    const opid = wx.getStorageSync('openID');
    if(opid){
      new Promise((resolve,reject)=>{
        wx.request({
          url: 'http://localhost:5555/users/getUserInfo',
          data:{
            openid:opid
          },
          success(res){
            resolve(res);
          },
          fail(err){
            reject(err)
          }
        })
      }).then((res)=>{
        const info = res.data.data[0];
        this.setData({
          userInfo:info
        })
        this.getCouPonData()
      })
    }else{
      console.log("您并没有登录！")
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getUserInfo()
  },
  goJiFengRecord(){
    wx.navigateTo({
      url: "/pages/JiFengRecord/JiFengRecord"
    })
  },
  goCoupon(){
    wx.navigateTo({
      url: "/pages/coupon/coupon"
    })
  },
  goshoppingMall(){
    wx.navigateTo({
      url: "/pages/shoppingMall/shoppingMall"
    })
  },
  getCouPonData(){
    const userid = wx.getStorageSync('openID');
    wx.request({
      url: 'http://localhost:5555/users/getUserCoupon',
      method:"get",
      data:{
        userid
      },
      success:(res)=>{
        this.setData({
          loading:false
        })
        const data = res.data;
        const count = data.reduce((sum,item)=>{
          return sum+item.num
        },0)
        this.setData({
          youhuijuan:count
        })
      }
    })
  }
})